<template>
  <div>
    <el-dialog
      title="绑定实体卡"
      :visible.sync="dialogVisible"
      width="750px"
      :close-on-click-modal="false"
      @close="close"
    >
      <el-form
        v-if="vipDetail"
        ref="form"
        :v-loading="loading"
        :model="form"
        :rules="rules"
        label-width="120px"
        size="small"
      >
        <el-form-item
          prop="balance"
          label="原实体卡号："
        >{{ vipDetail.entityCard&&vipDetail.entityCard.cardNo?vipDetail.entityCard.cardNo:"无" }}</el-form-item>
        <el-form-item prop="cardNo" label="新实体卡号：">
          <el-input v-model.trim="form.cardNo" placeholder="请输入实体卡号" maxlength="30" show-word-limit />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" :disabled="loading" round @click="hide">取 消</el-button>
        <el-button size="small" :disabled="loading" round type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { editEntityCardOps } from '@/api/vip'
export default {
  props: {
    vipDetail: {
      type: Object,
      require: true
    }
  },
  data() {
    return {
      // 校验字段相关
      rules: {
        cardNo: [{ required: true, message: '请输入实体卡号' }]
      },
      // 配置
      resolve: null,
      reject: null,
      // 状态
      dialogVisible: null,
      loading: false,
      // 数据
      form: {
        cardNo: null
      }
    }
  },
  methods: {
    /**
       * 弹框相关
       */
    open() {
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.loading = false
      this.form.cardNo = null
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
       * 数据组
       */

    /**
       * 按钮组
       */
    // 确 定
    submit() {
      this.$refs.form.validate((valid, object) => {
        if (valid) {
          if (
            this.vipDetail.entityCard &&
              this.form.cardNo == this.vipDetail.entityCard.cardNo
          ) {
            return
          }
          this.loading = true
          const params = {
            cardNo: this.form.cardNo,
            memberId: this.vipDetail.member.id,
            operationType: 4
          }
          editEntityCardOps(params)
            .then((res) => {
              this.$notify({
                title: '成功',
                message: '保存成功',
                type: 'success',
                duration: 2000,
                offset: 40
              })
              this.resolve && this.resolve()
              this.hide()
            })
            .catch((err) => {})
            .finally(() => {
              this.loading = false
            })
        }
      })
    }

    /**
       * 状态组
       */

    /**
       * 方法组
       */
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
